package customerEnter.Frame.Login;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import customerEnter.Frame.DaoDataTransmission.WebAppsInfo;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.filter.LoginFilter;
import customerEnter.Frame.redis.Major.RedisClient;
import customerEnter.tool.WhetherToEmpty;

public class LoginUserFiter implements Filter {

	private Map<String, String>	map;

	public void destroy() {
		/*
		 * RedisClient.getRedisTemplate().opsForValue().set("yez_easyBuyMall",
		 * "");
		 * RedisClient.getRedisTemplate().opsForValue().set("yez_easyBuyMall_social"
		 * , ""); RedisClient.getRedisTemplate().opsForValue().set(
		 * "yez_easyBuyMall_userSpace", "");
		 */// http://localhost:8090/yez_easyBuyMall_social/
	}

	@SuppressWarnings("unused")
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		// String weburl = request.getScheme() + "://" + request.getServerName()
		// + ":" + request.getServerPort() + "/"
		// + WebAppsInfo.webName + "/";
		//
		HttpServletRequest request2 = (HttpServletRequest) request;
		HttpServletResponse response2 = (HttpServletResponse) response;
		// CookieUtils.setCookie(request2, response2, "easyBuyMall_cookie",
		// "1231231", 1800);
		String uri = request2.getRequestURI();
		String path = uri.substring(uri.lastIndexOf("/")).replace("/", "");
		if (!WhetherToEmpty.StringWhetherToEmpty(map.get(path))) {
			Filter filter = new LoginFilter();
			LoginInvocationHandler loginInvocationHandler = new LoginInvocationHandler(filter, request, response);
			Filter proxy = (Filter) loginInvocationHandler.getProxy();
			proxy.doFilter(request, response, chain);
		} else {
			chain.doFilter(request, response);
		}

	}

	public void init(FilterConfig filterConfig) throws ServletException {
		setWebName();
		List<Map<String, String>> list = LoginXML.getLogXMl(WebAppsInfo.webUrl + "WEB-INF/classes/UserLogin.xml");

		if (WhetherToEmpty.ObjectWhetherToEmpty(map)) {
			map = new HashMap<String, String>();
		}

		String page = "";
		List<String> liStrings = new ArrayList<String>();

		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = list.get(i);
			if (i == 0) {
				for (Entry<String, String> entry : map.entrySet()) {
					page += entry.getKey() + ",";
				}
			} else if (i == 1) {
				for (Entry<String, String> entry : map.entrySet()) {
					if (!WhetherToEmpty.StringWhetherToEmpty(entry.getKey())) {
						liStrings.add(entry.getKey());
					}
				}
			}
		}
		String[] str = page.split(",");
		for (String string : str) {
			string = string.replace(" ", "");
			map.put(string, string);
		}

		for (String url : liStrings) {
			try {
				File root = new File(filterConfig.getServletContext().getRealPath("/") + url);
				showAllFiles(root);
			} catch (Exception e) {
				System.out.println("LOAD: " + url + "  FAILURE " + e.getMessage());
			}
		}
		System.out.println("------------------------------被攔截的路徑---------------------------------");
		for (Entry<String, String> entry : map.entrySet()) {
			System.out.println(entry.getKey());
		}
		System.out.println("------------------------------被攔截的路徑---------------------------------");
	}

	public void showAllFiles(File dir) throws Exception {
		File[] fs = dir.listFiles();
		for (int i = 0; i < fs.length; i++) {
			String url = fs[i].getAbsolutePath().substring(fs[i].getAbsolutePath().lastIndexOf("\\") + 1,
					fs[i].getAbsolutePath().length());
			map.put(url, url);
			if (fs[i].isDirectory()) {
				try {
					showAllFiles(fs[i]);
				} catch (Exception e) {
				}
			}
		}
	}

	@SuppressWarnings("static-access")
	public void setWebName() {

		RedisClient.getRedisTemplate().expire(WebAppsInfo.webName, 365, TimeUnit.DAYS);

		WebAppsInfo.webAppName.setWebServerUrl_Shopping(RedisClient.getRedisTemplate().opsForValue()
				.get("yez_easyBuyMall"));

		WebAppsInfo.webAppName.setWebServerUrl_social(RedisClient.getRedisTemplate().opsForValue()
				.get("yez_easyBuyMall_social"));

		WebAppsInfo.webAppName.setWebServerUrl_userSpace(RedisClient.getRedisTemplate().opsForValue()
				.get("yez_easyBuyMall_userSpace"));

		WebAppsInfo.webAppName.setWebServerUrl_userManager(RedisClient.getRedisTemplate().opsForValue()
				.get("yez_easyBuyMall_userManager"));

		Log4j.Log4jOutInfo(WebAppsInfo.webAppName.getWebServerUrl_Shopping());
		Log4j.Log4jOutInfo(WebAppsInfo.webAppName.getWebServerUrl_social());
		Log4j.Log4jOutInfo(WebAppsInfo.webAppName.getWebServerUrl_userSpace());

	}
}
